package com.tanhua.server.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.vo.Answers;
import com.tanhua.server.vo.TestSoulResultVo;
import com.tanhua.server.vo.TestSoulVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")
public class TestSoulController {
    @Autowired
    private TestSoulService testSoulService;


    /**
     * 测灵魂-问卷列表
     * @return
     */
    @GetMapping
    public ResponseEntity<List<TestSoulVo>> questionnaireList(){
        try {
            List<TestSoulVo> testSoulVoList = testSoulService.questionnaireList();
            if (CollUtil.isNotEmpty(testSoulVoList)){
                return ResponseEntity.ok(testSoulVoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 测灵魂-提交问卷
     * @param answers
     * @return
     */
    @PostMapping
    public ResponseEntity<String> subQuestionnaire(@RequestBody Map<String, List<Answers>> answers){
        try {
            List<Answers> answersList = answers.get("answers");
            String reportId = this.testSoulService.subQuestionnaire(answersList);
            if (StrUtil.isNotEmpty(reportId)){
                //将下一级进行解锁
                return ResponseEntity.ok(reportId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 测灵魂-查看结果
     * @param id 结果报告id
     * @return
     */
    @GetMapping("report/{id}")
    public ResponseEntity<TestSoulResultVo> queryReport(@PathVariable("id") Long id){
        try {
            TestSoulResultVo testSoulResultVo =  this.testSoulService.queryReport(id);
            if (ObjectUtil.isNotEmpty(testSoulResultVo)){
                return ResponseEntity.ok(testSoulResultVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
